import type { MesRouting, MesRoutingProcess, MesRoutingProduct } from "@/pages/mes/routing/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
	//工艺路线编号
	code?: string;
	//工艺路线名称
	name?: string;
	//状态
	status?: string;
	//当前页码
	page: number;
	//每页数量
	pageSize: number;
}

export interface PageListRes {
	//总记录数
	total: number;
	//数据列表
	records: MesRouting[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/mes/routing/pageList",
		data,
	});
const getInfo = (id: React.Key): Promise<MesRouting> =>
	apiClient.get({
		url: `/mes/routing/${id}`,
	});

const add = (data: MesRouting): Promise<MesRouting> =>
	apiClient.post({
		url: "/mes/routing",
		data,
	});

const update = (data: MesRouting): Promise<MesRouting> =>
	apiClient.put({
		url: "/mes/routing",
		data,
	});

const remove = (ids: React.Key[]): Promise<void> =>
	apiClient.delete({
		url: "/mes/routing",
		params: {
			ids: ids.join(","),
		},
	});
const getProcessList = (id: React.Key): Promise<MesRoutingProcess[]> =>
	apiClient.post({
		url: "/mes/routing/process/list",
		data: {
			routingId: id,
		},
	});

const getProducts = (id: React.Key): Promise<MesRoutingProduct[]> =>
	apiClient.post({
		url: "/mes/routing/product/list",
		data: {
			routingId: id,
		},
	});

const updateStatus = (ids: React.Key[], status: string): Promise<void> =>
	apiClient.put({
		url: `/mes/routing/status/${status}`,
		params: {
			ids: ids.join(","),
		},
	});

// 获取产品的工艺路线
const getProductRouting = (productId: React.Key): Promise<MesRouting[]> =>
	apiClient.get({
		url: `/mes/routing/product/selectList/${productId}`,
	});

export const mesRoutingService = {
	pageList,
	getInfo,
	add,
	update,
	remove,
	getProcessList,
	getproducts: getProducts,
	updateStatus,
	getProductRouting,
};

export default mesRoutingService;
